<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{ text }">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #qrcode="{ record }">
        <QrCode v-if="record.controlCode.enable !== 1" :value="record.qrcodeUrl" :number="1" width="100" />
        <a-button v-if="record.controlCode.enable === 1" type="link" @click="randomCode(record)">随机码</a-button>
      </template>
      <template #href="{ text }">
        <span>
          <TypographyLink :href="text" target="_blank" :copyable="{ text: text }"> 点击访问 </TypographyLink>
        </span>
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small"
          @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <QrcodeManageModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="com.cngzh.traceability.biz.qrcodemanage-qrcodeManage" setup>
import { onMounted, ref } from 'vue';
import { Typography } from 'ant-design-vue';
import { QrCode } from '/@/components/Qrcode/index';
import { BasicTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { defHttp } from '@/utils/http/axios';
import { useListPage } from '/@/hooks/system/useListPage';
import QrcodeManageModal from './components/QrcodeManageModal.vue';
import { columns, searchFormSchema } from './QrcodeManage.data';
import { list, deleteOne } from './QrcodeManage.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { useRouter } from 'vue-router';
const checkedKeys = ref<Array<string | number>>([]);
const router = useRouter();
const TypographyLink = Typography.Link;
//注册model
const [registerModal, { openModal }] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: 't_qrcode_manage',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 150,
      fixed: 'right',
    },
  },
});

const [registerTable, { reload, getColumns, setColumns }, { rowSelection, selectedRowKeys }] = tableContext;

onMounted(() => {
  getCodeInfo()
})

/**
 * 获取控码信息
 */
function getCodeInfo() {
  // 获取控码信息
  defHttp.get({ url: '/qrcodeManageControl/queryControlCode' }).then((res) => {
    if (res.enable) {
      const columns = getColumns();
      columns.map(item => {
        if (item.dataIndex == 'qrcodeUrl') {
          item.ifShow = false;
        }
      })
      setColumns(columns)
    }
  });
}

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}
/**
 * 详情
 */
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
 * 操作栏
 */
function getTableAction(record) {
  const action = [];
  action.push({
    label: '编辑',
    onClick: handleEdit.bind(null, record),
  });
  return action;
}
/**
 * 下拉操作栏
 */
function getDropDownAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    },
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
  ];
}

/**
 * 随机码
 * @param record
 */
function randomCode(record) {
  // 跳转到地块，并且携带参数
  router.push({
    path: '/qrcodemanage/QrcodeManageControl',
    query: { qmId: record.id },
  });
}
</script>

<style scoped></style>
